package com.shuoyue.ycgk.ui.account;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.UserInfo;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import io.reactivex.Observable;

public interface BindPhoneContract {

    interface View extends BaseView {
        void sendSuc(String phone);

        void bindSuc(UserInfo includeNull);
    }

    class Presenter extends BasePresenter<View> {
        private Model model = new Model();

        public void sendSms(String phone) {
            apply(model.sendSms(phone))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> s) {
                            mView.sendSuc(phone);
                        }
                    });

        }

        public void bindPhone(String openId,String accesstoken, String phone, String code,
                              String deviceId,
                              String deviceName) {
            apply(model.bindPhone(openId,accesstoken,phone,code,
                      deviceId,
                      deviceName))
                    .subscribe(new ApiSubscriber<Optional<UserInfo>>(mView, this) {
                        @Override
                        public void onNext(Optional<UserInfo> stringOptional) {
                            super.onNext(stringOptional);
                            mView.bindSuc(stringOptional.getIncludeNull());
                        }
                    });
        }
    }

    class Model extends BaseModel {
        public Observable<BaseResult<String>> sendSms(String phone) {
            return RetrofitClient.getInstance().getApi().smsSend(phone);
        }
        public Observable<BaseResult<UserInfo>> bindPhone(String openId,String accesstoken, String phone, String code,
                                                          String deviceId,
                                                          String deviceName) {
            return RetrofitClient.getInstance().getApi().bindPhone(openId,accesstoken,phone,code,
                      deviceId,
                      deviceName);
        }
    }

}
